 //501.二叉搜索树中的众数
 //https://leetcode.cn/problems/find-mode-in-binary-search-tree/
class Solution {
    //众数 多个众数的出现频率相同
public:
    vector<int> findMode(TreeNode* root) {
        if(!root) return {};
        TreeNode* prev = nullptr;
        vector<int> ret;
        int count = 0,maxcount = 0;
        function<void(TreeNode*)> dfs = [&](TreeNode* root){
            if(!root) return;
            dfs(root->left);

            if(prev == nullptr) count = 1; //第一个
            else if(prev->val == root->val) ++count; //==
            else count = 1; //!=

            if(count == maxcount) ret.push_back(root->val);
            if(count > maxcount)
            {
                maxcount = count;
                ret.clear();
                ret.push_back(root->val);
            }
            prev = root;
            dfs(root->right);
        };
        dfs(root);
        return ret;
    }
};